package com.salt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.salt.common.Result;
import com.salt.entity.User;
import com.salt.service.IncomeDetailService;
import com.salt.utils.UserContext;
import com.salt.vo.IncomeDetailVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;

/**
 * 收入明细控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/income-details")
@RequiredArgsConstructor
public class IncomeDetailController {

    private final IncomeDetailService incomeDetailService;

    /**
     * 分页查询收入明细
     *
     * @param page 页码
     * @param size 每页大小
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<IPage<IncomeDetailVO>> pageQuery(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            // 获取当前登录用户
            User user = UserContext.getUser();
            if (user == null) {
                return Result.error("用户未登录");
            }

            // 创建分页对象
            Page<IncomeDetailVO> pageParam = new Page<>(current, size);

            // 查询收入明细
            IPage<IncomeDetailVO> result = incomeDetailService.pageQuery(pageParam, user.getInviteCode());

            return Result.success(result);
        } catch (Exception e) {
            log.error("分页查询收入明细失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/summary")
    public Result<Map<String, Object>> getIncomeSummary(
            @RequestParam(required = false)  String startDate,
            @RequestParam(required = false)  String endDate) {

        log.info("接收到CPA佣金明细汇总查询请求，开始日期：{}，结束日期：{}", startDate, endDate);

        try {
            // 获取当前用户
            User user = UserContext.getUser();
            if (user == null) {
                return Result.error("用户未登录");
            }

            // 转换日期字符串为LocalDate
       /*     LocalDate start = StringUtils.isNotEmpty(startDate)
                    ? LocalDate.parse(startDate)
                    : new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            LocalDate end = StringUtils.isNotEmpty(endDate)  // 注意：这里应该判断 endDate 是否为空
                    ? LocalDate.parse(endDate)
                    : new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            // 转换为Date类型，并设置时间范围
            Date startDateTime = java.util.Date.from(
                    start.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()
            );
            Date endDateTime = java.util.Date.from(
                    end.atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()
            );*/

            Map<String, Object> summary = incomeDetailService.getIncomeSummary(null, null, user.getInviteCode());
            return Result.success(summary);
        } catch (Exception e) {
            log.error("查询CPA佣金明细汇总数据失败", e);
            return Result.error(e.getMessage());
        }
    }
} 